//
//  PhotoBrowserCell.swift
//  SinaWeibo
//
//  Created by 林柱强 on 16/2/18.
//  Copyright © 2016年 林柱强. All rights reserved.
//

import UIKit
import SDWebImage

protocol PhotoBrowserCellDelegate: NSObjectProtocol {
    
    func photoBrowserCellDidClose(cell: PhotoBrowserCell)
}

class PhotoBrowserCell: UICollectionViewCell {

    weak var photoBrowserCellDelegate: PhotoBrowserCellDelegate?
    var imageUrl: NSURL?
        {
            didSet{
                reset()
                iconView.sd_setImageWithURL(imageUrl) { (image, _, _, _) -> Void in
                    //调整图片位置
                    self.setImageViewPostion()
                }
            }
    }
    
    /**
     重置scrollview和imageview的属性
     */
    private func reset() {
        //重置scrollview属性
        scrollview.contentInset = UIEdgeInsetsZero
        scrollview.contentOffset = CGPointZero
        scrollview.contentSize = CGSizeZero
        
        //重置imageview属性
        iconView.transform = CGAffineTransformIdentity
    }
    

    /**
     调整图片位置
     */
    private func setImageViewPostion() {
        //拿到按照宽高比计算之后的图片大小
        let size = self.displaySize(iconView.image!)
        //判断图片高度，是否大于屏幕高度
        if size.height < UIScreen.mainScreen().bounds.height {
            //小于 为短图 居中显示
            iconView.frame = CGRect(origin: CGPointZero, size: size)
            //处理居中显示
            let y = (UIScreen.mainScreen().bounds.height - size.height) * 0.5
            self.scrollview.contentInset = UIEdgeInsets(top: y, left: 0, bottom: y, right: 0)
        }else {
            //大于 为长图，设置scrollview的滚动范围为图片大小
            iconView.frame = CGRect(origin: CGPointZero, size: size)
            scrollview.contentSize = size
        }
        
        
        
    }
    
    
    /**
     按照图片的宽高比显示图片的大小
    */
    private func displaySize(image: UIImage) -> CGSize {
        //拿到宽高比
        let scale = image.size.height / image.size.width
        //根据宽高计算高度
        let height = screenWidth * scale
        
        return CGSize(width: screenWidth, height: height)
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    private func setupUI() {
        //添加控件
        contentView.addSubview(scrollview)
        scrollview.addSubview(iconView)
        
        //控件布局
        scrollview.frame = UIScreen.mainScreen().bounds
        
        //设置图片缩放
        scrollview.delegate = self
        scrollview.maximumZoomScale = 2.0
        scrollview.minimumZoomScale = 0.5
        
        //设置监听事件
        let tap = UITapGestureRecognizer(target: self, action: "close")
        iconView.addGestureRecognizer(tap)
        iconView.userInteractionEnabled = true
        
    }
    
    func close() {
        photoBrowserCellDelegate?.photoBrowserCellDidClose(self)
    }
    
    //MARK: - l懒加载

    private var scrollview: UIScrollView = UIScrollView()
    var iconView: UIImageView = UIImageView()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension PhotoBrowserCell: UIScrollViewDelegate {
    //告诉系统需要缩放的控件
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return iconView
    }
    
    //重新调整配图位置
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
        
        var offsetX = (screenWidth - view!.frame.width) * 0.5
        var offsetY = (screenHeight - view!.frame.height) * 0.5
        offsetX = offsetX < 0 ? 0 : offsetX
        offsetY = offsetY < 0 ? 0 : offsetY
        
        scrollView.contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: offsetY, right: offsetX)
    }
}
